﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PalcoCentral
{
    public partial class pcSelectDisc : Form
    {
        Database.person p;
        public pcSelectDisc(Database.person p)
        {
            InitializeComponent();
            this.p = p;
           
        }

        private void listaPessoas_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.saveSPButton.PerformClick();
        }

        private void searchPessoasET_KeyDown(object sender, KeyEventArgs e)
        {
           
        }


        private void pcSelectDisc_Load(object sender, EventArgs e)
        {
            refreshList();
        }

        private void refreshList()
        {
            searchDiscET.Text = "";
            searchSelectPessoa.PerformClick();
        }

        private void searchSelectPessoasET_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.searchSelectPessoa.PerformClick();
            }
        }

        private void searchSelectPessoa_Click(object sender, EventArgs e)
        {
            Library.OperationStatus os = Database.CourseDBAdapter.getInstance().searchCourse(searchDiscET.Text);
            if (os.sucess)
            {
                List<Database.course> res = (List<Database.course>)os.res;
                listaDisciplinas.DataSource = res;
                listaDisciplinas.Update();
                if (res != null && res.Count > 0)
                {
                    //personDB.DataSource = res;

                }
                else
                {
                    Library.Utils.atentionFields("Não forma encontrados resultados.", "Atenção");
                    //No Results Found
                }
            }
        }

        private void saveSPButton_Click(object sender, EventArgs e)
        {
            Database.course course = (Database.course)listaDisciplinas.SelectedItem;
            if (course == null)
            {
                return;
            }
            Library.OperationStatus os;
            if (this.p.personRole.CompareTo(Library.PersonRole.Student.ToString()) == 0)
            {
                os = Database.CourseDBAdapter.getInstance().addPersonToCourse(this.p, course.courseId, Library.PersonRole.Student);
            }
            else
            {
                os = Database.CourseDBAdapter.getInstance().addPersonToCourse(this.p, course.courseId, Library.PersonRole.Teacher);
            }
                if (os.sucess)
            {
                Library.Utils.sucessFields("Disciplina associada ao - " + this.p.personName + " - com sucesso.","Associar disciplina");
                //course = (Database.course) os.res;
            }
            else
            {
                Library.Utils.errorFields("Não foi possivel associar a disciplina a essa pessoa. Possivel causa: " + os.errorMessage, "Erro a associar disciplina");
                return;
            }
            cancelSPButton.PerformClick();
        }

        private void cancelSPButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public Database.course selectedPerson // retrieving a value from
        {
            get;
            set;
        }

        private void searchDiscET_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
